/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Computador
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command Calcular;
    private Command cancelCommand;
    private Command backCommand;
    private Command backCommand1;
    private Command Entrar;
    private Command backCommandHome;
    private Command cancelCommand1;
    private Command okCommand;
    private Command backCommand2;
    private Form CalcIPTU;
    private TextField valorv;
    private ChoiceGroup choiceGroup;
    private StringItem stringItem;
    private Form ResultIPTU;
    private StringItem valorv2;
    private StringItem valIPTU;
    private Form Home;
    private StringItem stringItem1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getHome());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
        switchDisplayable(null, getHome());//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == CalcIPTU) {//GEN-BEGIN:|7-commandAction|1|30-preAction
            if (command == Calcular) {//GEN-END:|7-commandAction|1|30-preAction

                switchDisplayable(null, getResultIPTU());//GEN-LINE:|7-commandAction|2|30-postAction
                // write post-action user code here
               double v = Double.parseDouble(valorv.getString());
                 double iptu = 0;
                 //Calculando o valor do IPTU residencial com os acréscimos e descontos ja embutidos.
          if(choiceGroup.isSelected(0)){
             if (v <= 81762){
                iptu = v * 0.008;

            }else if (v>=81762 && v<=163525){
                iptu = v * 0.01;

            }else if (v>=163525 && v<=327050){
                iptu = v * 0.012;

            }else if (v>=327050 && v<=654100){
                iptu = v * 0.014;

            }else{
                iptu = v * 0.016;
            }
             //Calculando o valor do IPTU não-residencial com os acréscimos e descontos ja embutidos.
         }else if (choiceGroup.isSelected(1)){
            if (v <= 100255){
                iptu = v * 0.012;

            }else if (v>=100255 && v<=200250){
                iptu = v * 0.014;

            }else if (v>=200250 && v<=400900){
                iptu = v * 0.016;

            }else if (v>=400900 && v<=801800){
                iptu = v * 0.018;

            }else{iptu = v * 0.02;
            }
            //Calculando o valor do IPTU de terrenos com os acréscimos e descontos ja embutidos.
         }else if (choiceGroup.isSelected(2)){
              if (v <= 100255){
                iptu = v * 0.012;

            }else if (v>=100255 && v<=200250){
                iptu = v * 0.014;

            }else if (v>=200250 && v<=400900){
                iptu = v * 0.016;

            }else if (v>=400900 && v<=801800){
                iptu = v * 0.018;

            }else{iptu = v * 0.02;
         }
    }
                //Exibindo valores.
              valorv2.setText(Double.toString(v));
              valIPTU.setText(Double.toString(iptu));
            } else if (command == backCommand1) {//GEN-LINE:|7-commandAction|3|43-preAction
                // write pre-action user code here
                switchDisplayable(null, getHome());//GEN-LINE:|7-commandAction|4|43-postAction
                // write post-action user code here
            } else if (command == cancelCommand1) {//GEN-LINE:|7-commandAction|5|87-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|87-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|95-preAction
        } else if (displayable == Home) {
            if (command == backCommand2) {//GEN-END:|7-commandAction|7|95-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|8|95-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|9|90-preAction
                // write pre-action user code here
                switchDisplayable(null, getCalcIPTU());//GEN-LINE:|7-commandAction|10|90-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|37-preAction
        } else if (displayable == ResultIPTU) {
            if (command == backCommand) {//GEN-END:|7-commandAction|11|37-preAction
                // write pre-action user code here
                switchDisplayable(null, getCalcIPTU());//GEN-LINE:|7-commandAction|12|37-postAction
                // write post-action user code here
            } else if (command == cancelCommand) {//GEN-LINE:|7-commandAction|13|34-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|14|34-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|7-postCommandAction
        }//GEN-END:|7-commandAction|15|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|16|
    //</editor-fold>//GEN-END:|7-commandAction|16|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CalcIPTU ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of CalcIPTU component.
     * @return the initialized component instance
     */
    public Form getCalcIPTU() {
        if (CalcIPTU == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            CalcIPTU = new Form("Calcular IPTU:", new Item[] { getStringItem(), getValorv(), getChoiceGroup() });//GEN-BEGIN:|14-getter|1|14-postInit
            CalcIPTU.addCommand(getCalcular());
            CalcIPTU.addCommand(getBackCommand1());
            CalcIPTU.addCommand(getCancelCommand1());
            CalcIPTU.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return CalcIPTU;
    }
    //</editor-fold>//GEN-END:|14-getter|2|
    //</editor-fold>



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Calcular ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of Calcular component.
     * @return the initialized component instance
     */
    public Command getCalcular() {
        if (Calcular == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            Calcular = new Command("Calcular", Command.OK, 0);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return Calcular;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            cancelCommand = new Command("Sair", Command.CANCEL, 0);//GEN-LINE:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|36-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: valorv ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of valorv component.
     * @return the initialized component instance
     */
    public TextField getValorv() {
        if (valorv == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            valorv = new TextField("Valor venal:", "", 32, TextField.ANY);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return valorv;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of choiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            choiceGroup = new ChoiceGroup("Tipo de propriedade:", Choice.EXCLUSIVE);//GEN-BEGIN:|23-getter|1|23-postInit
            choiceGroup.append("Residencial", null);
            choiceGroup.append("N\u00E3o-residencial", null);
            choiceGroup.append("Terreno", null);
            choiceGroup.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceGroup.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return choiceGroup;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ResultIPTU ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of ResultIPTU component.
     * @return the initialized component instance
     */
    public Form getResultIPTU() {
        if (ResultIPTU == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            ResultIPTU = new Form("IPTU", new Item[] { getValorv2(), getValIPTU() });//GEN-BEGIN:|31-getter|1|31-postInit
            ResultIPTU.addCommand(getCancelCommand());
            ResultIPTU.addCommand(getBackCommand());
            ResultIPTU.setCommandListener(this);//GEN-END:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return ResultIPTU;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: valorv2 ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of valorv2 component.
     * @return the initialized component instance
     */
    public StringItem getValorv2() {
        if (valorv2 == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            valorv2 = new StringItem("Valor venal: R$", "", Item.PLAIN);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return valorv2;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: valIPTU ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of valIPTU component.
     * @return the initialized component instance
     */
    public StringItem getValIPTU() {
        if (valIPTU == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            valIPTU = new StringItem("IPTU: R$", "");//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return valIPTU;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            backCommand1 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|42-getter|2|


















    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Entrar ">//GEN-BEGIN:|74-getter|0|74-preInit
    /**
     * Returns an initiliazed instance of Entrar component.
     * @return the initialized component instance
     */
    public Command getEntrar() {
        if (Entrar == null) {//GEN-END:|74-getter|0|74-preInit
            // write pre-init user code here
            Entrar = new Command("Entrar", Command.OK, 0);//GEN-LINE:|74-getter|1|74-postInit
            // write post-init user code here
        }//GEN-BEGIN:|74-getter|2|
        return Entrar;
    }
    //</editor-fold>//GEN-END:|74-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommandHome ">//GEN-BEGIN:|77-getter|0|77-preInit
    /**
     * Returns an initiliazed instance of backCommandHome component.
     * @return the initialized component instance
     */
    public Command getBackCommandHome() {
        if (backCommandHome == null) {//GEN-END:|77-getter|0|77-preInit
            // write pre-init user code here
            backCommandHome = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|77-getter|1|77-postInit
            // write post-init user code here
        }//GEN-BEGIN:|77-getter|2|
        return backCommandHome;
    }
    //</editor-fold>//GEN-END:|77-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|84-getter|0|84-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|84-getter|0|84-preInit
            // write pre-init user code here
            stringItem = new StringItem("Insira o valor venal e selecione o tipo da propriedade para calcular o IPTU.", " ");//GEN-LINE:|84-getter|1|84-postInit
            // write post-init user code here
        }//GEN-BEGIN:|84-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|84-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand1 ">//GEN-BEGIN:|86-getter|0|86-preInit
    /**
     * Returns an initiliazed instance of cancelCommand1 component.
     * @return the initialized component instance
     */
    public Command getCancelCommand1() {
        if (cancelCommand1 == null) {//GEN-END:|86-getter|0|86-preInit
            // write pre-init user code here
            cancelCommand1 = new Command("Cancelar", Command.CANCEL, 0);//GEN-LINE:|86-getter|1|86-postInit
            // write post-init user code here
        }//GEN-BEGIN:|86-getter|2|
        return cancelCommand1;
    }
    //</editor-fold>//GEN-END:|86-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            okCommand = new Command("Entrar", Command.OK, 0);//GEN-LINE:|89-getter|1|89-postInit
            // write post-init user code here
        }//GEN-BEGIN:|89-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|89-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Home ">//GEN-BEGIN:|85-getter|0|85-preInit
    /**
     * Returns an initiliazed instance of Home component.
     * @return the initialized component instance
     */
    public Form getHome() {
        if (Home == null) {//GEN-END:|85-getter|0|85-preInit
            // write pre-init user code here
            Home = new Form("IPTU SP", new Item[] { getStringItem1() });//GEN-BEGIN:|85-getter|1|85-postInit
            Home.addCommand(getOkCommand());
            Home.addCommand(getBackCommand2());
            Home.setCommandListener(this);//GEN-END:|85-getter|1|85-postInit
            // write post-init user code here
        }//GEN-BEGIN:|85-getter|2|
        return Home;
    }
    //</editor-fold>//GEN-END:|85-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|92-getter|0|92-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|92-getter|0|92-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Descri\u00E7\u00E3o:", "O aplicativo calcula o IPTU, da cidade de S\u00E3o Paulo, atrav\u00E9s do valor venal e tipo de propriedade fornecidos pelo usu\u00E1rio.");//GEN-LINE:|92-getter|1|92-postInit
            // write post-init user code here
        }//GEN-BEGIN:|92-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|92-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of backCommand2 component.
     * @return the initialized component instance
     */
    public Command getBackCommand2() {
        if (backCommand2 == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            backCommand2 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return backCommand2;
    }
    //</editor-fold>//GEN-END:|94-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
